<?php

require_once 'components/dataset/dataset.php';
require_once 'components/utils/string_utils.php';
require_once 'components/utils/system_utils.php';

class DatasetUtils
{
    public static function RowIterator(Dataset $dataSet)
    {

    }

    public static function SetDatasetFieldValue(Dataset $dataSet, $fieldName, $value)
    {
        if (!StringUtils::IsNullOrEmpty($fieldName))
            $dataSet->SetFieldValueByName($fieldName, $value);
    }

    public static function FormatDatasetFieldsTemplate(Dataset $dataset, $template, IDelegate $processValue = null)
    {
        $result = $template;
        foreach($dataset->GetFields() as $field)
        {
            if ($dataset->IsLookupField($field->GetNameInDataset()))
            {
                $result = StringUtils::ReplaceVariableInTemplate(
                    $result,
                    $field->GetAlias(),
                    $processValue == null ?
                        $dataset->GetFieldValueByName($field->GetAlias()) :
                        $processValue->Call($dataset->GetFieldValueByName($field->GetAlias()), $field->GetAlias())
                    );
            }
            else
            {
                $result = StringUtils::ReplaceVariableInTemplate(
                    $result,
                    $field->GetName(),
                    $processValue == null ?
                        $dataset->GetFieldValueByName($field->GetNameInDataset()) :
                        $processValue->Call($dataset->GetFieldValueByName($field->GetNameInDataset()), $field->GetNameInDataset())
                    );
            }
        }
        return $result;
    }
}

?>